#include "scriptcontext.h"
#include "loader.h"
#include "pluginmanager.h"

namespace nickel
{
	namespace core
	{
		ScriptContext::ScriptContext(void)
		{
		}
		
		ScriptContext::~ScriptContext(void)
		{
		}

		void ScriptContext::Compile()
		{

		}

		void ScriptContext::Run()
		{
			HandleScope handleScope;
			Handle<String> someString = String::New("abcdefg");
			Persistent<Context> context = Context::New(NULL, CreateGlobalTemplate());
	
			Context::Scope context_scope(context);

			char buffer[255];
			g_SMAPI->Format(buffer, 255, "%s/script.js", g_SMAPI->GetBaseDir());
		
			//Script::Compile(framework::FileSystem::ReadFile(buffer))->Run();

			delete[] buffer;

			context.Dispose();
		}

		Handle<ObjectTemplate> ScriptContext::CreateGlobalTemplate()
		{
			Handle<ObjectTemplate> global = ObjectTemplate::New();

			PluginManager pluginManager;
	
			global->Set(String::New("register"), FunctionTemplate::New(pluginManager.Register()));

			return global;
		}

		/*Handle<Value> Include(const Arguments& args) 
		{
			for (int i = 0; i < args.Length(); i++) 
			{
				String::Utf8Value str(args[i]);

				std::string js_file = load_file(*str);

				if(js_file.length() > 0) 
				{
					Handle<String> source = String::New(js_file.c_str());
					Handle<Script> script = Script::Compile(source);
					
					return script->Run();
				}
			}
			
			return Undefined();
		}*/
	}
}